odoo.define('web_optimize.HierarchyController', function (require) {
"use strict";

/*---------------------------------------------------------
 * Odoo Hierarchy view
 *---------------------------------------------------------*/

const AbstractController = require('web.AbstractController');
const { _t } = require('web.core');
const { qweb } = require('web.core');


var HierarchyController = AbstractController.extend({
    events: _.extend({}, AbstractController.prototype.events, {
        'click button.save-image': '_onSaveImage',
        'click button.fixed-image': '_onZoomToFix',
        'click button.center-image': '_onCenterImage',
        "click .expand-node": "_onExpandNode",
        "click .collapse-node": "_onCollapseNode"
    }),


    /**
     * @override
     * @param {Widget} parent
     * @param {GraphModel} model
     * @param {GraphRenderer} renderer
     * @param {Object} params
     */
    init: function (parent, model, renderer, params) {
        this._super.apply(this, arguments);
    },
    /**
     * @todo check if this can be removed (mostly duplicate with
     * AbstractController method)
     */
    destroy: function () {
        if (this.$buttons) {
            // remove jquery's tooltip() handlers
            this.$buttons.find('button').off().tooltip('dispose');
        }
        this._super.apply(this, arguments);
    },
    /**
     * Render the buttons according to the Hierarchy.buttons and
     * add listeners on it.
     * Set this.$buttons with the produced jQuery element
     *
     * @param {jQuery} [$node] a jQuery node where the rendered buttons should
     * be inserted $node may be undefined, in which case the GraphView does
     * nothing
     */
    renderButtons: function ($node) {
        this.$buttons = $(qweb.render('Hierarchy.buttons'));
        this.$buttons.find('button').tooltip();
        this.$buttons.appendTo(this.$el.find(".o_cp_buttons"));
    },
     _onSaveImage: function (e) {
        e.stopPropagation();
        var svgstr = this.renderer.chart.makeSvg({ scale: 1, background: "white" }).outerHTML;
        var blob = new Blob([svgstr], { type: "image/svg+xml" });
        this._downlandBlob(blob, "employee_org.svg");
     },
    _downlandBlob(blob, fileName) {
        var url = window.URL.createObjectURL(blob);
        var filename = fileName;

        var a = document.createElement("a");
        a.style = "display: none";
        a.href = url;
        a.download = filename;

        if (window.navigator.msSaveBlob !== undefined) {
            window.navigator.msSaveBlob(blob, filename);
            return;
        }
        document.body.appendChild(a);
        requestAnimationFrame(function() {
            a.click();
            window.URL.revokeObjectURL(url);
            document.body.removeChild(a);
        });
    },
     _onZoomToFix: function (e) {
        e.stopPropagation();
        this.renderer.chart.commandHandler.zoomToFit();
    },
     _onCenterImage: function (e) {
        e.stopPropagation();
        this.renderer.chart.scale = 1;
        this.renderer.chart.commandHandler.scrollToPart(this.renderer.chart.findNodeForKey(1));
    },
    _onExpandNode: function (e) {
        e.stopPropagation();
        var nodes = this.renderer.chart.nodes;
        this.renderer.chart.startTransaction("expandNodes");
        while(nodes.next()){
            var node = nodes.value;
            if (!node.isTreeExpanded){
                node.isTreeExpanded = true;
            }
        }
        this.renderer.chart.commitTransaction("expandNodes");

    },
    _onCollapseNode: function (e) {
        e.stopPropagation();
        var nodes = this.renderer.chart.findTreeRoots();
        this.renderer.chart.startTransaction("collapseNodes");
        this.renderer.chart.findTreeRoots().each(function (node) {
            node.collapseTree(2);
        });
        this.renderer.chart.commitTransaction("collapseNodes");
    }

});

return HierarchyController;

});
